<?php

namespace coser;

use coser\Config;

class Controller
{
	public static $smarty = [];
	
	public function __construct()
	{
		vendor('smarty/smarty/libs/Smarty.class.php');
		//检查smarty是否实例化
        if(!is_object(self::$smarty)){
			self::$smarty = new \Smarty();
        }		
	}
	
	//输出视图模板
	public function display($templates)
	{
		//查找视图模板
		$temp_dir = $this->findTemplates($templates);
		
		if(!file_exists($temp_dir)){
			exit('模板文件不存在'.$temp_dir);
		}
		
		//为模板赋值基础变量
		self::$smarty->assign('__STATIC__',__STATIC__);
		
		self::$smarty->display($temp_dir);
	}
	
	//赋值视图模板
	public function assign($key,$val)
	{
		self::$smarty->assign($key,$val);//赋值
	}
	
	//查找视图模板
	public function findTemplates($templates)
	{
		if(file_exists($templates)){
			return $templates;
		}else{
			$request = Request::instance();
			$temp_dir = APP_PATH . $request->module() . '/view/' .$templates.'.html';
			return $temp_dir;
		}
	}
	

}